package com.detrum.sportcamera.runnable;

import android.os.Handler;

import com.detrum.base.BaseStatic;
import com.detrum.sportcamera.finals.NfsNetFinal;
import com.detrum.utils.DeviceFinal;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.TimerTask;

/**
 * Created by abc on 2017/12/1.
 */

public class HeartRunnable extends TimerTask {
    DatagramSocket socket;
    Handler handler;

    public HeartRunnable(DatagramSocket socket, Handler handler) {
        this.socket = socket;
        this.handler =handler;
    }

    @Override
    public void run() {
        try {
            byte[] head = new byte[]{0x14, 0x0c, 0x0b, 0x0b};
            byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.HEART};
            byte[] heartData = new byte[12];
            System.arraycopy(head, 0, heartData, 0, head.length);
            heartData[5] = (byte) camHead.length;
            System.arraycopy(camHead, 0, heartData, 8, camHead.length);
            long checkSum = SendOrder.checkSum(heartData, heartData.length);
            heartData[6] = (byte)(( checkSum >> 8 ) & 0xFF);
            heartData[7] = (byte)(checkSum  & 0xFF);
            DatagramPacket inPacket = new DatagramPacket(heartData, heartData.length , InetAddress.getByName(BaseStatic.DHCP_SERVICE_IP) , DeviceFinal.SEND_DATA_PORT);
            socket.send(inPacket);//把数据发送到服务端。

        }catch (IOException e) {
            e.printStackTrace();
            handler.sendEmptyMessage(300);
        }
    }
}
